package com.gree.changying.server.network.upload;

import android.content.Context;
import com.gree.changying.utils.StringUtil;

import com.gree.changying.Const;
import com.gree.changying.server.network.http.AsyncHttpClient;
import com.gree.changying.server.network.http.AsyncHttpResponseHandler;
import com.gree.changying.server.network.http.RequestParams;
import com.gree.changying.server.utils.NLog;
import com.gree.changying.server.utils.SharedPreferencesUtil;

import org.apache.http.Header;

import java.io.File;
import java.util.concurrent.CountDownLatch;

/**
 * Create by 游德禄 on 2019-10-18
 */
public class UploadProgram implements Runnable {


    //安装 上传文件的地址
    private String uploadPath = "";
    private String updatePath;

    private OnThreadResultListener listener;//任务线程回调接口
    private UploadFile uploadFile;
    private AsyncHttpClient asyncHttpClient;

    private CountDownLatch countDownLatch = new CountDownLatch(1);


    public UploadProgram(Context context, String domain, String updateFile, UploadFile uploadFile, OnThreadResultListener listener){
        this.uploadFile = uploadFile;
        this.listener=listener;
        this.asyncHttpClient = AsyncHttpClient.getInstance(context);
        this.uploadPath = domain;
        this.updatePath = updateFile;
    }


    @Override
    public void run() {
       // NLog.e("dsdfsdfs",uploadPath,uploadFile.getFilePath());
        if(StringUtil.isEmpty(uploadFile.getFilePath())){
            listener.onInterrupted(-2,"文件路径为空!");
            countDownLatch.countDown();
        }else{
            File file = new File(uploadFile.getFilePath());
            if(file.isFile()){
                try {
                    RequestParams params = new RequestParams();
                    params.put("file",file);
                    //params.put("updateFilePath",updatePath);
                    String token = SharedPreferencesUtil.getData(Const.TOKEN,null);
                    if(token!=null){
                        asyncHttpClient.addHeader("Authorization","Bearer  "+token);
                    }
                    asyncHttpClient.post(uploadPath,params,new AsyncHttpResponseHandler(){
                        @Override
                        public void onProgress(int bytesWritten, int totalSize) {
                            super.onProgress(bytesWritten, totalSize);
                            float pressent = (float) bytesWritten / totalSize * 100;
                            listener.onProgressChange((int)pressent,bytesWritten,totalSize);
                        }

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                            super.onSuccess(statusCode, headers, responseBody);
                            listener.onFinish(new String(responseBody));
                            countDownLatch.countDown();
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                            super.onFailure(statusCode, headers, responseBody, error);
                            NLog.e("dfdfgdfgdf",error.getMessage());
                            listener.onInterrupted(statusCode,error.getMessage());
                            countDownLatch.countDown();
                        }
                    });
                }catch (Exception e){
                    listener.onInterrupted(0,e.getMessage());
                    countDownLatch.countDown();
                }
            }else {
                listener.onInterrupted(-1,"这不是文件!");
                countDownLatch.countDown();
            }
        }



        try {
            //等待
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
